package com.szgz.platform.model;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;

@Data
@TableName("user_resource")
public class UserResource {
    
    @TableId(value = "resource_id", type = IdType.ASSIGN_UUID)
    private String resourceId;
    
    @TableField("user_id")
    private String userId;
    
    @TableField("cpu_limit")
    private Integer cpuLimit;
    
    @TableField("memory_limit")
    private Integer memoryLimit;
    
    @TableField("disk_limit")
    private Integer diskLimit;
    
    @TableField("network_limit")
    private Integer networkLimit;
    
    @TableField("current_cpu_usage")
    private Integer currentCpuUsage;
    
    @TableField("current_memory_usage")
    private Integer currentMemoryUsage;
    
    @TableField("current_disk_usage")
    private Integer currentDiskUsage;
    
    @TableField("current_network_usage")
    private Integer currentNetworkUsage;
    
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    @TableField("updated_at")
    private LocalDateTime updatedAt;
    
    @TableLogic
    @TableField("deleted")
    private Boolean deleted;
    
    // 构造函数
    public UserResource() {
        this.deleted = false;
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    // 构造函数
    public UserResource(String userId, Integer cpuLimit, Integer memoryLimit, Integer diskLimit) {
        this();
        this.userId = userId;
        this.cpuLimit = cpuLimit;
        this.memoryLimit = memoryLimit;
        this.diskLimit = diskLimit;
    }
    
    // 更新资源使用情况
    public void updateUsage(Integer cpuUsage, Integer memoryUsage, Integer diskUsage, Integer networkUsage) {
        this.currentCpuUsage = cpuUsage;
        this.currentMemoryUsage = memoryUsage;
        this.currentDiskUsage = diskUsage;
        this.currentNetworkUsage = networkUsage;
        this.updatedAt = LocalDateTime.now();
    }
    
    // 检查是否超过CPU限制
    public boolean isCpuExceeded() {
        return this.cpuLimit != null && this.currentCpuUsage != null && 
               this.currentCpuUsage > this.cpuLimit;
    }
    
    // 检查是否超过内存限制
    public boolean isMemoryExceeded() {
        return this.memoryLimit != null && this.currentMemoryUsage != null && 
               this.currentMemoryUsage > this.memoryLimit;
    }
    
    // 检查是否超过磁盘限制
    public boolean isDiskExceeded() {
        return this.diskLimit != null && this.currentDiskUsage != null && 
               this.currentDiskUsage > this.diskLimit;
    }
    
    // 获取CPU使用率百分比
    public double getCpuUsagePercentage() {
        if (this.cpuLimit == null || this.currentCpuUsage == null) {
            return 0.0;
        }
        return (double) this.currentCpuUsage / this.cpuLimit * 100;
    }
    
    // 获取内存使用率百分比
    public double getMemoryUsagePercentage() {
        if (this.memoryLimit == null || this.currentMemoryUsage == null) {
            return 0.0;
        }
        return (double) this.currentMemoryUsage / this.memoryLimit * 100;
    }
    
    // 获取磁盘使用率百分比
    public double getDiskUsagePercentage() {
        if (this.diskLimit == null || this.currentDiskUsage == null) {
            return 0.0;
        }
        return (double) this.currentDiskUsage / this.diskLimit * 100;
    }
    
    // 检查是否有资源限制
    public boolean hasResourceLimits() {
        return this.cpuLimit != null || this.memoryLimit != null || this.diskLimit != null;
    }
    
    // 重置资源使用情况
    public void resetUsage() {
        this.currentCpuUsage = 0;
        this.currentMemoryUsage = 0;
        this.currentDiskUsage = 0;
        this.currentNetworkUsage = 0;
        this.updatedAt = LocalDateTime.now();
    }
} 